<?php

/*************************************************************************

	FILE INCLUDE

**************************************************************************/
include_once '../lib/Template.class.php';
$debug_mode_value = 0;

/*************************************************************************

	USER SCRIPT

***************************************************************************/
$userscript =<<<js
    <script type="text/javascript">
        <!--

        //-->
    </script>
js;


/*************************************************************************

		PAGE MAIN PROCESS

***************************************************************************/
(!empty($_REQUEST['search_ner']))? $search_ner = $_REQUEST['search_ner'] : $search_ner = "";
(!empty($_REQUEST['search_domain']))? $search_domain = $_REQUEST['search_domain'] : $search_domain = "";
(!empty($_REQUEST['search_status']))? $search_status = $_REQUEST['search_status'] : $search_status = "";
(!empty($_REQUEST['search_cluster_1']))? $search_cluster_1 = $_REQUEST['search_cluster_1'] : $search_cluster_1 = "";
(!empty($_REQUEST['search_cluster_2']))? $search_cluster_2 = $_REQUEST['search_cluster_2'] : $search_cluster_2 = "";
(!empty($_REQUEST['search_candidate']))? $search_candidate = htmlspecialchars($_REQUEST['search_candidate']) : $search_candidate = "";

$query_add="";

if(!empty($search_ner)){
	$query_add .= "AND a.NER_CD =  $search_ner ";
	$link_add .= "&search_ner=$search_ner";
}
if(!empty($search_domain)){
	$query_add .= "AND a.DOMAIN_CD = $search_domain ";
	$link_add .= "&search_domain=$search_domain";
}
if(!empty($search_cluster_1)){
	$query_add .= "AND a.CLUSTER_CD >=  $search_cluster_1 ";
	$link_add .= "&search_cluster_1=$search_cluster_1";
}
if(!empty($search_cluster_2)){
	$query_add .= "AND a.CLUSTER_CD <=  $search_cluster_2 ";
	$link_add .= "&search_cluster_2=$search_cluster_2";
}
if(!empty($search_candidate)){
	$query_add .= "AND a.CLUSTER_CD in (SELECT CLUSTER_CD FROM candidate_tb WHERE CANDIDATE_NM='$search_candidate')";
	$link_add .= "&search_candidate=$search_candidate";
}
if(!empty($search_status)){
	$query_add .= "AND a.CLUSTER_ST = $search_status ";
	$link_add .= "&search_status=$search_status";
}

$db->conn();

$sql = "select b.CANDIDATE_NM as C1,d.CANDIDATE_NM as C2
		from cluster_tb a
		  inner join candidate_tb b
		  on a.CLUSTER_CD = b.CLUSTER_CD
      AND b.REPRESENT_FL=0
		    left outer join ner_tb c
		    on a.NER_CD = c.NER_CD
          left outer join candidate_tb d
           on a.CLUSTER_CD = d.CLUSTER_CD
           AND d.REPRESENT_FL=1
		where a.CLUSTER_ST = 8 $query_add
		order by a.CLUSTER_CD DESC,b.REPRESENT_FL DESC";
$rs = $db->query($sql);
$num = $db->num($rs);
$csv_list="";

if( $num > 0 )
{
	for( $i = 0; $i < $num; $i++)
	{
		$temp = $db->fetch_assoc($rs);
		$csv_list.=trim($temp[C1])." -> ".trim($temp[C2])."\r\n";
	}
}

$db->close();

$file_name="Synonym_TXT_".date("Ymd_His").".txt";
ob_end_clean();
header("Content-type: txt;charset=euc-kr" );
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Description: PHP4 Generated Data" );

echo(mb_convert_encoding($csv_list, "euc-kr","auto"));
